<script setup lang="ts">
import { Handle, Position } from '@vue-flow/core';
import { NIcon, NPopconfirm } from 'naive-ui';
import { Close, Flow } from '@vicons/carbon';

interface NodeData {
  label: string;
  mutexId?: string;
}

const props = defineProps<{
  id: string;
  data: NodeData;
}>();

const emit = defineEmits<{
  deleteNode: [nodeId: string];
  addCondition: [mutexId: string];
}>();

function handleAddCondition() {
  emit('addCondition', props.data.mutexId || props.id);
}

function handleConfirmDelete() {
  emit('deleteNode', props.id);
}
</script>

<template>
  <div class="mutex-decision-node">
    <!-- 顶部输入连接点 -->
    <Handle type="target" :position="Position.Top" class="handle" />

    <!-- 删除按钮 -->
    <NPopconfirm
      placement="top"
      negative-text="取消"
      positive-text="确定"
      @positive-click="handleConfirmDelete"
    >
      <template #trigger>
        <button class="delete-btn" title="删除互斥节点">
          <NIcon :size="12">
            <Close />
          </NIcon>
        </button>
      </template>
      确定要删除此互斥节点吗？这将删除所有条件分支。
    </NPopconfirm>

    <!-- 添加条件按钮 -->
    <div class="add-condition-btn" @click="handleAddCondition">
      <NIcon :size="16" color="white">
        <Flow />
      </NIcon>
      <span>{{ data.label || '添加条件' }}</span>
    </div>

    <!-- 底部输出连接点（会有多个，动态生成） -->
    <Handle type="source" :position="Position.Bottom" class="handle" />
  </div>
</template>

<style scoped>
.mutex-decision-node {
  min-width: 160px;
  background: white;
  border-radius: 20px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  position: relative;
  overflow: visible;
}

.delete-btn {
  position: absolute;
  top: -8px;
  right: -8px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  border: none;
  background: #ef4444;
  border-radius: 50%;
  cursor: pointer;
  color: white;
  padding: 0;
  z-index: 10;
  opacity: 0;
  transition: all 0.2s;
}

.mutex-decision-node:hover .delete-btn {
  opacity: 1;
}

.add-condition-btn {
  background: #3b82f6;
  color: white;
  padding: 12px 20px;
  border-radius: 20px;
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.2s;
}

.add-condition-btn:hover {
  background: #2563eb;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.4);
}

.handle {
  width: 8px;
  height: 8px;
  background: #3b82f6;
  border: 2px solid white;
  border-radius: 50%;
}
</style>
